package org.dromara.business.controller;

import lombok.RequiredArgsConstructor;
import org.dromara.business.domain.vo.ShoppingCartVo;
import org.dromara.business.service.ShoppingCartService;
import org.dromara.common.core.domain.R;
import org.dromara.common.web.core.BaseController;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 购物车
 *
 * @author zlj
 * @date 2024-08-10
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/business/cart")
public class ShoppingCartController extends BaseController {

    private final ShoppingCartService shoppingCartService;

    /**
     * 查询购物车列表
     */
    @GetMapping("/list")
    public R<List<ShoppingCartVo>> list() {
        return R.ok(shoppingCartService.queryList());
    }

    /**
     * 购物车新增商品
     *
     * @param goodsMallId 商品id
     */
    @PostMapping("/add")
    public R<Void> add(@RequestParam Long goodsMallId, @RequestParam Integer isAdd) {
        return toAjax(shoppingCartService.add(goodsMallId, isAdd));
    }

    /**
     * 清空购物车
     */
    @DeleteMapping()
    public R<Void> clear() {
        return toAjax(shoppingCartService.clear());
    }

    /**
     * 清空购物车
     */
    @DeleteMapping("/removeCartGoods")
    public R<Void> removeCartGoods(Long goodsId) {
        return toAjax(shoppingCartService.removeCartGoods(goodsId));
    }

    @DeleteMapping("/remove/{shoppingCartIds}")
    public R<Void> remove(@PathVariable Long[] shoppingCartIds) {
        return toAjax(shoppingCartService.removeBatchByIds(Arrays.asList(shoppingCartIds)));
    }
}
